/*
 * $Id: ColorPicker.java,v 1.1 2003/05/20 21:12:21 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.gui;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

/**
 * Displays simple localized color picker.
 *
 * @author Tomas Znamenacek
 */
public class ColorPicker extends JDialog {

	protected JButton ok;
	protected JColorChooser colorChooser;

	protected boolean cancelled;

	/** Localised resource strings. */
	protected static ResourceBundle resources;
	
	/** Set the color displayed in color chooser. */
	public void setColor(Color activeColor) {

		colorChooser.setColor(activeColor);
	}

	/** Returns color the user chose in color chooser. */
	public Color getColor() {

		return colorChooser.getColor();
	}

	/** Was the dialog cancelled? */
	public boolean cancelled() {

		return cancelled;
	}

	/** Creates the color picker. */
	public ColorPicker(JFrame parent) {
	
		super(parent, resources.getString("picker.title"), true);
		colorChooser = new JColorChooser();
		
		// Button bar
		JPanel buttonBar = new JPanel();
		buttonBar.setLayout(new FlowLayout());
		
		JButton cancel = new JButton(resources.getString("button.cancel"));
		cancel.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {
						cancelled = true;
						dispose();
					}
				});
		buttonBar.add(cancel);

		ok = new JButton(resources.getString("button.ok"));
		ok.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent a) {
						cancelled = false;
						dispose();
					}
				});
		buttonBar.add(ok);

		// Dump it out
		Container canvas = getContentPane();
		canvas.setLayout(new BorderLayout());
		canvas.add(buttonBar, BorderLayout.SOUTH);
		canvas.add(colorChooser, BorderLayout.CENTER);
	
		// Adjust size
		pack();
		setSize(getPreferredSize());
		setResizable(false);
			
		// Center
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int) ((screenDim.width - getWidth()) / 2), (int) ((screenDim.height - getHeight()) / 2));
	}

	public void show() {

		getRootPane().setDefaultButton(ok);
		super.show();
	}
	
	static {
		
		resources = ResourceBundle.getBundle("net.sf.vofce.gui.resources");
	}
}
